/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xdialer.cpp
 *
 * Purpose: xdialer define and implementation
 *
 * Developer:
 *   wen.gu , 2023-10-30
 *
 * TODO:
 *
 ***************************************************************************/

#include "xbus/com/xdialer.h"
#include "xbus/com/xmessage_impl.h"
#include "xbus/com/internal/xdialer_impl.h"

namespace xbus::com {

using namespace xbus::core;

XDialer::XDialer(XSessionSp session)
    :session_(session) {
    //todo something
}

XDialer::~XDialer() {
    //todo something
}

XResultCode XDialer::start(const XClientStartParam& param) {
    if (session_) { 
        reconnect_time_interval_ms_ = param.reconnect_time_interval_ms;
        return session_->start(param.session_param);
    }

    return XResultCode::FromError(XErrC::InvalidStatus);
}

XResultCode XDialer::stop() {
    if (session_) { 
        return session_->stop();
    }

    return XResultCode::FromError(XErrC::InvalidStatus);
}

XResult<XMessageSp> XDialer::alloc(XMessageType type, MessageId msg_id, uint32_t payload_size, const uint8_t* payload /*= nullptr*/) {
    XMessageSp msg = XMessageImpl::CreateMsg(payload_size, payload);

    if (msg) {
        msg->set_type(type);
        msg->set_message_id(msg_id);
        return msg;
    }

    return XResult<XMessageSp>::FromError(XErrC::InsufficientReousces);
}


XResultCode XDialer::send(XMessageSp msg) {
    if (session_) { 
        return session_->send(msg);
    }

    return XResultCode::FromError(XErrC::InvalidStatus);
}

XResult<XMessageSp> XDialer::recv(int32_t wait_ms /*= core::WaitType::WaitForever*/) {
    if (session_) { 
        return session_->recv(wait_ms);
    }

    return XResult<XMessageSp>::FromError(XErrC::InvalidStatus);
}


//static 
XResult<XDialerSp> XDialer::FindService(const std::string& url, int32_t port /*= 0*/, int32_t timeout_ms /*= core::WaitType::NoWait*/) {
    return internal::XDialerImpl::getInstance().tryConnect(url, port, timeout_ms);
}

//static 
XResult<FindServiceHandle> XDialer::StartFindService(const std::string& url, int32_t port, OnFindServiceHandler handler) {
    auto res = internal::XDialerImpl::getInstance().registerConnection(url, port, handler);
    if (res) {    
        return FindServiceHandle{ res.value()}; 
    }

    return XResult<FindServiceHandle>::FromError(res.error());
}

//static 
XResultCode XDialer::StopFindService(const FindServiceHandle& handle) {
    return internal::XDialerImpl::getInstance().unregisterConnection(handle.uid);
}


} //namespace xbus::com